package com.tools.utils;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class HardwareUtil {


    /**
     * 获取主板序列号，支持Windows和Linux系统
     */
    public String getMotherboardSerial() {
        String os = System.getProperty("os.name").toLowerCase();

        try {
            if (os.contains("windows")) {
                return getWindowsMotherboardSerial();
            } else if (os.contains("linux")) {
                return getLinuxMotherboardSerial();
            } else {
                log.warn("不支持的操作系统: {}", os);
                return "UNKNOWN";
            }
        } catch (Exception e) {
            log.error("获取主板序列号失败", e);
            return "UNKNOWN";
        }
    }

    /**
     * Windows系统通过WMI命令获取主板序列号
     */
    private String getWindowsMotherboardSerial() throws Exception {
        Process process = Runtime.getRuntime().exec("wmic baseboard get serialNumber");
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)
        );

        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (!line.isEmpty() && !line.equals("SerialNumber")) {
                log.debug("Windows主板序列号: {}", line);
                return line;
            }
        }

        reader.close();
        process.waitFor();
        return "UNKNOWN";
    }

    /**
     * Linux系统通过dmidecode命令获取主板序列号
     */
    private String getLinuxMotherboardSerial() {
        try {
            // 优先使用dmidecode命令
            Process process = Runtime.getRuntime().exec("sudo dmidecode -s baseboard-serial-number");
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)
            );

            String line = reader.readLine();
            reader.close();
            process.waitFor();

            if (line != null && !line.trim().isEmpty() && !line.contains("Not Specified")) {
                log.debug("Linux主板序列号: {}", line.trim());
                return line.trim();
            }

            // 备选方案：读取系统文件
            return getLinuxMotherboardFromSys();

        } catch (Exception e) {
            log.error("dmidecode命令执行失败", e);
            return getLinuxMotherboardFromSys();
        }
    }

    /**
     * 从/sys/class/dmi/id/board_serial文件读取主板序列号
     */
    private String getLinuxMotherboardFromSys() {
        try {
            Process process = Runtime.getRuntime().exec("cat /sys/class/dmi/id/board_serial");
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)
            );

            String line = reader.readLine();
            reader.close();
            process.waitFor();

            if (line != null && !line.trim().isEmpty()) {
                log.debug("Linux主板序列号(从sys读取): {}", line.trim());
                return line.trim();
            }

        } catch (Exception e) {
            log.warn("从/sys文件读取失败", e);
        }

        return "UNKNOWN";
    }

    /**
     * 获取系统信息摘要，用于调试和展示
     */
    public String getSystemInfo() {
        return String.format("操作系统: %s %s, 架构: %s, 主板序列号: %s",
                System.getProperty("os.name"),
                System.getProperty("os.version"),
                System.getProperty("os.arch"),
                getMotherboardSerial()
        );
    }

    public static void main(String[] args) {
        // HardwareUtil 测试
        HardwareUtil hardwareUtil = new HardwareUtil();
        try {
            System.out.println(hardwareUtil.getMotherboardSerial());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
